﻿using System;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using WeifenLuo.WinFormsUI.Docking;

namespace GgCarControl
{
    public partial class AutoSendCarStatusForm : DockContent
    {
        private Database db = new OracleDatabase("Data Source=DG;User ID=dgrfid;Password=dgrfid;Unicode=True;Load Balance Timeout=1000");
       
        public AutoSendCarStatusForm()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            string strBegin = this.dtpBegin.Value.ToString("yyyy-MM-dd HH:mm:00");//开始时间
            string strEnd = this.dtpEnd.Value.ToString("yyyy-MM-dd HH:mm:59");//结束时间
            string strSQL = "";
            //汇总
            strSQL = @"select stackname 垛名,getowner 收货人,count(id) 放车数 from v_truck_cargo_login_all 
                       where coalesce(isauto,'0')='1' and truckstatus not in ('0','1','9') and coalesce(cancel_flag,'0')<>'1' and logintime>=to_date('{0}', 'yyyy-mm-dd hh24:mi:ss') and logintime<=to_date('{1}','yyyy-mm-dd hh24:mi:ss')
                       group by stackname,getowner 
                       union all
                       select '合计' as 垛名,'' as 收货人,count(id) as 放车数
                       from v_truck_cargo_login_all 
                       where coalesce(isauto,'0')='1' and truckstatus not in ('0','1','9') and coalesce(cancel_flag,'0')<>'1' and logintime>=to_date('{0}', 'yyyy-mm-dd hh24:mi:ss') and logintime<=to_date('{1}','yyyy-mm-dd hh24:mi:ss')";
            strSQL = string.Format(strSQL, strBegin, strEnd);
            this.dgvHZ.DataSource = db.ExecuteDataSet(db.GetSqlStringCommand(strSQL)).Tables[0];

            //明细
            strSQL = @"select logintime as 登记时间,truckno as 车号,SHIPNAME as 船名,cargoname as 货名,BILL_NO as 提单号,SHIPPER_NAME as 货主代理,SETOWNER as 代运人,
                              GETOWNER as 收货人,yard as 地点,ZYJX as 作业机械,stackname as 垛名,CARDADD as 登记地点,PACKAGE as 包装,REMARK as 备注,WEIGHT as 重量,
                              PIECES as 件数,DECODE(LYBZ, '1', '物流网导入', '停车场登记') as 数据来源,BAR_CODE as 条码号 from v_truck_cargo_login_all 
                       where coalesce(isauto,'0')='1' and truckstatus not in ('0','1','9') and coalesce(cancel_flag,'0')<>'1' 
                             and logintime>=to_date('{0}', 'yyyy-mm-dd hh24:mi:ss') and logintime<=to_date('{1}','yyyy-mm-dd hh24:mi:ss')";
            strSQL = string.Format(strSQL, strBegin, strEnd);
            this.dgvMX.DataSource = db.ExecuteDataSet(db.GetSqlStringCommand(strSQL)).Tables[0];
        }

        private void AutoSendCarStatusForm_Load(object sender, EventArgs e)
        {
            InitDateTime();
            LoadData();
        }

        private void InitDateTime()
        {
            if (DateTime.Now.Hour >= 6 && DateTime.Now.Hour <= 8)//早上交班时间
            {
                this.dtpBegin.Value = DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 17:00"));
                this.dtpEnd.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 7:00"));
            }
            if (DateTime.Now.Hour >= 16 && DateTime.Now.Hour <= 18)//晚上交班时间
            {
                this.dtpBegin.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 7:00"));
                this.dtpEnd.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 17:00"));
            }
        }
    }
}
